from  deslab import *
from minimal_basis import *

"""
This is the script for the computation of minimal event bases that ensure diagnosability.
The algorithms are part of the paper:
Basilio, J., Lima, S., Lafortune, S.,, e Moreira, M. (2012), "Computation of minimal
event bases that ensure diagnosability," Discrete Event Dynamic Systems,
leo@2012
"""
               

def find_EDES(Gd): 
    """ This is an implementation of algorithm 1 for
    finding FPES using a depth-first-search approach
    with lexicographical order and queuing reset. The input is
    automaton Gd and the output is the set EDES and the tree 
    representing the search of FPES """    
    
    
    SUCC = Gd.Graph.successors # we are accessing the successor of node 
    XYN = [xd for xd in Gd if YN_type(xd)=='YN']
    XYNY = [xd_yn for xd_yn in XYN if any(YN_type(xd) =='Y' for xd in SUCC(xd_yn))]
    trees = dict()   
    U_FPES = set()      
    FPES = []     
    for xdyn in XYNY:
        FPES, tree = find_FPES(Gd,xdyn)  
        trees[xdyn] = tree 
        U_FPES |= FPES      
    EDES = prodcollection(U_FPES)    
    EDES = minimals_of_poset(EDES)
    return EDES, trees

""" TESTING CODE"""
#
Gd =load('Gd_example3')

draw(Gd,'figurecolor')



